package javawdw.thread;

/**
 * 线程引导工具类
 * 
 * @author 王大卫
 * 
 */
public class Bootstrap {
	/**
	 * 引导给定可引导对象execute方法
	 * 
	 * @param bootable
	 *            可引导对象
	 */
	public static void boot(final Bootable bootable) {
		bootable.execute();
	}

	/**
	 * 循环引导execute方法，每引导一次休息1ms
	 * 
	 * @param bootable
	 *            可引导对象
	 */
	public static void loop(Bootable bootable) {
		loop(bootable, 1);
	}

	/**
	 * 循环引导execute方法
	 * 
	 * @param bootable
	 *            可引导对象
	 * @param interval
	 *            每引导一次休息间隔（ms）
	 */
	public static void loop(final Bootable bootable, final int interval) {
		Runnable runnable = new Runnable() {
			public void run() {
				while (true) {
					try {
						bootable.execute();
						Thread.sleep(interval);
					} catch (InterruptedException e) {
						break;
					}
				}
			}
		};
		Thread thread = new Thread(runnable);
		thread.start();
	}
}